<template>
  <div class="newProject">
    <div style="width: 95%;height: 838px; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 500;font-size: 16px;color: #303133;line-height: 24px;">编辑任务</span>
      </div>
      <div>
        <el-form ref="form" :model="taskInfo" :rules="rules">
          <el-row type="flex" justify="space-between" v-show="hasProject"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">是否有所属项目：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="radio">
                <el-radio-group v-model="radio" :disabled="canRadio">
                  <el-radio :label="1">是</el-radio>
                  <el-radio :label="2">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>所属执行项目：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-select @change="projectChange" :disabled="radio == 2" v-model="taskInfo.projectId"
                  placeholder="请输入所属执行项目" style="width: 100%; ">
                  <el-option v-for="item in projectOptions" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务名称：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="name">
                <el-input v-model="taskInfo.name" placeholder="请输入任务名称" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">任务类型：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskType">
                <el-select v-model="taskInfo.taskType" placeholder="请选择任务类型" style="width: 100%; ">
                  <el-option v-for="item in taskTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">开始时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="releaseTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="taskInfo.releaseTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="">完成时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="endTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="taskInfo.endTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务下达性质：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="nature">
                <el-select v-model="taskInfo.nature" placeholder="请选择任务下达性质" style="width: 100%; ">
                  <el-option v-for="item in natureOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">承办部门：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="undertakeDeptId">
                <el-select v-model="taskInfo.undertakeDeptId" placeholder="请选择承办部门" style="width: 100%; ">
                  <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <!-- <el-cascader :options="deptOptions" :show-all-levels="false" v-model="taskInfo.undertakeDeptId"
                  placeholder="请选择承办部门" :props="{ checkStrictly: true }" style="width: 100%;" clearable></el-cascader> -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务量：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="taskCount">
                <el-input v-model="taskInfo.taskCount" placeholder="请输入任务量" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">任务单位：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-select v-model="taskInfo.taskUnit" placeholder="请选择任务单位" style="width: 100%; ">
                <el-option v-for="item in taskUnitOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">经费下达总额(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="feeTotal">
                <el-input v-model="taskInfo.feeTotal" placeholder="请输入经费下达总额(万元)"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">经费下达单价(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="feeUnitPrice">
                <el-input v-model="taskInfo.feeUnitPrice" placeholder="请输入经费下达单价(万元)"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">任务编号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="projectNo">
                <el-input :disabled="true" style="height: 28px !important;" v-model="projectNo"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <!-- <el-input v-model="form.no" placeholder="请输入任务编号" style="height: 28px !important;"></el-input> -->
                <el-input v-model.number="taskNo" placeholder="请输入任务编号(正整数)" :precision="0" controls-position="right"
                  style="height: 28px !important;"
                  oninput="value = value.replace(/[^\d]/g, '');if(value.length>9); value=value.slice(0,9)"
                  :min="0"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>任务描述：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入任务描述" v-model="taskInfo.taskContent">
              </el-input>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>备注：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="taskInfo.remark">
              </el-input>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">确定</el-button>
            <el-button type="primary" @click="closeDialog"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<script>
import { editThisTask, getTaskNo } from '@/api/project/task.js'
import { getDeptList, getProjectList } from "@/api/project/project";
export default {
  components: {
  },
  props: {
    taskInfo: {
      type: Object,
      required: false,
    }
  },
  data() {
    return {
      canRadio: false,
      hasProject: true,
      radio: 1,
      form: {
        projectId: '',
        name: '',
        taskUnit: '',
        taskType: '',
        taskCount: '',
        releaseTime: '',
        endTime: '',
        nature: '',
        undertakeDeptId: '',
        feeTotal: '',
        feeUnitPrice: '',
        no: '',
        taskContent: '',
        remark: ''
      },
      taskUnitOptions: [],
      projectOptions: [],
      taskTypeOptions: [],
      natureOptions: [],
      deptOptions: [],
      inputDisabled: true,
      rules: {
        radio: [
          { required: true, message: '请选择是否有所属项目', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
        ],
        taskType: [
          { required: true, message: '请选择任务类型', trigger: 'blur' },
        ],
        releaseTime: [
          { required: true, message: '请选择开始时间', trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '请选择开始时间', trigger: 'blur' },
        ],
        nature: [
          { required: true, message: '请选择任务下达性质', trigger: 'blur' },
        ],
        undertakeDeptId: [
          { required: true, message: '请选择承办部门', trigger: 'blur' },
        ],
        taskCount: [
          { required: true, message: '请输入任务量', trigger: 'blur' },
        ],
        taskUnit: [
          { required: true, message: '请输入任务单位', trigger: 'blur' },
        ],
        feeTotal: [
          { required: true, message: '请输入经费下达总额', trigger: 'blur' },
        ],
        feeUnitPrice: [
          { required: true, message: '请输入经费下达单价', trigger: 'blur' },
        ],
        projectNo: [
          { required: true, message: '请输入任务编号', trigger: 'blur' },
        ],
      },
      projectNo: '',
      taskNo: 1
    }
  },
  watch: {
    radio(newRadio) {
      if (newRadio == 2) {
        this.projectNo = ''
        this.form.projectId = ''
      }
    }
  },
  mounted() {
    let proId = this.$route.query.id
    if (proId) {
      this.radio = 1
      this.canRadio = true
      this.taskInfo.projectId = proId
      getTaskNo(proId).then(res => {
        this.taskNo = res.data
        let arr = res.data.split("-");
        this.projectNo = arr[0]
        this.taskNo = parseInt(arr[1])
        console.log('this.taskNo', this.taskNo)
      })
    }
    getDeptList().then(res => {
      this.deptOptions = res.data[0].children
    });
    getProjectList().then(res => {
      this.projectOptions = res.rows
    })
    this.natureOptions = this.getDict('xmgl_task_nature');
    this.taskTypeOptions = this.getDict('xmgl_task_type');
    this.taskUnitOptions = this.getDict('xmgl_task_unit');
  },
  methods: {
    projectChange() {
      let proId = this.taskInfo.projectId
      getTaskNo(proId).then(res => {
        this.taskNo = res.data
        let arr = res.data.split("-");
        this.projectNo = arr[0]
        this.taskNo = parseInt(arr[1])
      })
    },
    onSubmit() {
      // this.taskInfo.undertakeDeptId = this.taskInfo.undertakeDeptId[this.taskInfo.undertakeDeptId.length - 1]
      // this.form.overallCompletionRate = 0;

      console.log('this.taskInfo.undertakeDeptId', this.taskInfo.undertakeDeptId)
      this.taskInfo.no = this.projectNo + '-' + this.taskNo
      editThisTask(this.taskInfo).then(res => {
        if (res.code = 200) {
          alert("编辑任务成功！")
          this.closeDialog()

        }
      });
    },
    loadEdit() {
      if (this.taskInfo.projectId) {
        this.radio = 1
      } else {
        this.radio = 2
        let arr = this.taskInfo.no.split("-");
        this.projectNo = arr[0]
        this.taskNo = parseInt(arr[1])
      }
      console.log(this.taskInfo, this.projectNo, this.taskNo)
    },
    closeDialog() {
      this.$emit('close-dialog'); // 触发自定义事件
    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      console.log(dict)
      return dict
    }
  }
}
</script>

<style scoped lang="scss">
.newProject {
  width: 95%;
  height: 100%;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    text-align: center;
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
